package com.bw.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.base.BaseApp;
import com.bw.gmall.realtime.common.bean.TradeSkuOrderBean;
import com.bw.gmall.realtime.common.constant.Constant;
import com.bw.gmall.realtime.common.util.DateFormatUtil;
import com.bw.gmall.realtime.common.util.FlinkSinkUtil;
import com.bw.gmall.realtime.common.util.HbaseUtil;
import com.bw.gmall.realtime.dws.function.DorisMapFunction;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.hadoop.hbase.client.Connection;

import java.math.BigDecimal;
import java.time.Duration;

/*
12.9交易域SKU粒度下单各窗口汇总表   同步缓存
 */

public class DwsTradeSkuOrderSyncCacheWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsTradeSkuOrderSyncCacheWindow().start(Constant.TOPIC_DWD_TRADE_ORDER_DETAIL,
                Constant.DWS_TRADE_SKU_ORDER_WINDOW,4,10029);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> dataStreamSource) {
        // 1.etl清洗数据
        SingleOutputStreamOperator<JSONObject> eltStream = elt(dataStreamSource);

        // 2.设置水位线
        SingleOutputStreamOperator<JSONObject>watermarksStream = getWatermarksStream(eltStream);

        // 3.同一笔详情订单分组,计算对应指标
        SingleOutputStreamOperator<TradeSkuOrderBean> processStream = getProcessStream(watermarksStream);

        // 4.按照商品分组聚合
        SingleOutputStreamOperator<TradeSkuOrderBean> reduceStream = getReduceStream(processStream);
//        reduceStream.print();

        // 5.同步关联
        SingleOutputStreamOperator<TradeSkuOrderBean> result = getResult(reduceStream);
//        result.print();

        // 6.写到doris
        result.map(new DorisMapFunction<>())
                .sinkTo(FlinkSinkUtil.getDorisSink(Constant.DWS_TRADE_SKU_ORDER_WINDOW));

    }

    private static SingleOutputStreamOperator<TradeSkuOrderBean> getResult(SingleOutputStreamOperator<TradeSkuOrderBean> reduceStream) {
        return reduceStream.map(new RichMapFunction<TradeSkuOrderBean, TradeSkuOrderBean>() {

            Connection hbaseConnect;

            @Override
            public void open(Configuration parameters) throws Exception {
                hbaseConnect = HbaseUtil.getHbaseConnect();
            }

            @Override
            public void close() throws Exception {
                hbaseConnect.close();
            }

            @Override
            public TradeSkuOrderBean map(TradeSkuOrderBean tradeSkuOrderBean) throws Exception {
                //sku
                String skuId = tradeSkuOrderBean.getSkuId();
                JSONObject skuInfo = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_sku_info", skuId);
                String spuId = skuInfo.getString("spu_id");
                String category3Id = skuInfo.getString("category3_id");
                String tmId = skuInfo.getString("tm_id");
                tradeSkuOrderBean.setTrademarkId(tmId);
                tradeSkuOrderBean.setCategory3Id(category3Id);
                tradeSkuOrderBean.setSpuId(spuId);

                //spu
                JSONObject spuInfo = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_spu_info", spuId);
                tradeSkuOrderBean.setSpuName(spuInfo.getString("spu_name"));

                //tm
                JSONObject dimBaseTrademark = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_base_trademark", tmId);
                tradeSkuOrderBean.setTrademarkName(dimBaseTrademark.getString("tm_name"));

                //c3
                JSONObject dimBaseCategory3 = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_base_category3", category3Id);
                tradeSkuOrderBean.setCategory3Name(dimBaseCategory3.getString("name"));
                String category2Id = dimBaseCategory3.getString("category2_id");
                tradeSkuOrderBean.setCategory2Id(category2Id);

                //c2
                JSONObject dimBaseCategory2 = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_base_category2", category2Id);
                tradeSkuOrderBean.setCategory2Name(dimBaseCategory2.getString("name"));
                String category1Id = dimBaseCategory2.getString("category1_id");
                tradeSkuOrderBean.setCategory1Id(category1Id);

                //c1
                JSONObject dimBaseCategory1 = HbaseUtil.getCells(hbaseConnect, Constant.HBASE_NAMESPACE, "dim_base_category1", category1Id);
                tradeSkuOrderBean.setCategory1Name(dimBaseCategory1.getString("name"));

                return tradeSkuOrderBean;
            }
        });
    }

    private static SingleOutputStreamOperator<TradeSkuOrderBean> getReduceStream(SingleOutputStreamOperator<TradeSkuOrderBean> processStream) {
        return processStream.keyBy(x -> x.getSkuId())
                .window(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(5)))
                .reduce(new ReduceFunction<TradeSkuOrderBean>() {
                            @Override
                            public TradeSkuOrderBean reduce(TradeSkuOrderBean x, TradeSkuOrderBean y) throws Exception {
                                x.setOriginalAmount(x.getOriginalAmount().add(y.getOriginalAmount()));
                                x.setActivityReduceAmount(x.getActivityReduceAmount().add(y.getActivityReduceAmount()));
                                x.setCouponReduceAmount(x.getCouponReduceAmount().add(y.getCouponReduceAmount()));
                                x.setOrderAmount(x.getOrderAmount().add(y.getOrderAmount()));
                                return x;
                            }
                        },
                        new ProcessWindowFunction<TradeSkuOrderBean, TradeSkuOrderBean, String, TimeWindow>() {
                            @Override
                            public void process(String s, ProcessWindowFunction<TradeSkuOrderBean, TradeSkuOrderBean, String, TimeWindow>.Context context, Iterable<TradeSkuOrderBean> iterable, Collector<TradeSkuOrderBean> collector) throws Exception {
                                TradeSkuOrderBean tradeSkuOrderBean = iterable.iterator().next();
                                tradeSkuOrderBean.setStt(DateFormatUtil.tsToDateTime(context.window().getStart()));
                                tradeSkuOrderBean.setEdt(DateFormatUtil.tsToDateTime(context.window().getEnd()));
                                tradeSkuOrderBean.setCurDate(DateFormatUtil.tsToDateTime(System.currentTimeMillis()));
                                collector.collect(tradeSkuOrderBean);
                            }
                        });
    }

    private static SingleOutputStreamOperator<TradeSkuOrderBean> getProcessStream(SingleOutputStreamOperator<JSONObject> watermarksStream) {
        return watermarksStream.keyBy(x -> x.getString("id"))
                .process(new KeyedProcessFunction<String, JSONObject, TradeSkuOrderBean>() {
                    MapState<String, BigDecimal> lastOrderState;

                    @Override
                    public void open(Configuration parameters) {
                        MapStateDescriptor<String, BigDecimal> stateDescriptor = new MapStateDescriptor<>("lastOrderState", String.class, BigDecimal.class);
                        //设置状态存储时间
                        stateDescriptor.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(30)).build());
                        lastOrderState = getRuntimeContext().getMapState(stateDescriptor);
                    }

                    @Override
                    public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, TradeSkuOrderBean>.Context context, Collector<TradeSkuOrderBean> collector) throws Exception {
                        //从状态中取出对应指标的值
                        BigDecimal originalAmount = lastOrderState.get("originalAmount");
                        BigDecimal activityReduceAmount = lastOrderState.get("activityReduceAmount");
                        BigDecimal couponReduceAmount = lastOrderState.get("couponReduceAmount");
                        BigDecimal orderAmount = lastOrderState.get("orderAmount");
                        //判断对应的值
                        originalAmount = originalAmount == null ? new BigDecimal(0) : originalAmount;
                        activityReduceAmount = activityReduceAmount == null ? new BigDecimal(0) : activityReduceAmount;
                        couponReduceAmount = couponReduceAmount == null ? new BigDecimal(0) : couponReduceAmount;
                        orderAmount = orderAmount == null ? new BigDecimal(0) : orderAmount;

                        //从数据中拿值
                        //原始金额要用数量乘单价计算
                        BigDecimal newOriginalAmount = jsonObject.getBigDecimal("order_price").multiply(jsonObject.getBigDecimal("sku_num"));
                        BigDecimal newActivityReduceAmount = jsonObject.getBigDecimal("split_activity_amount");
                        BigDecimal newCouponReduceAmount = jsonObject.getBigDecimal("split_coupon_amount");
                        BigDecimal newOrderAmount = jsonObject.getBigDecimal("split_total_amount");

                        //直接传入下游的指标有
                        String id = jsonObject.getString("id");
                        String skuId = jsonObject.getString("sku_id");
                        String skuName = jsonObject.getString("sku_name");
                        Long ts = jsonObject.getLong("ts");

                        //为防止回撤流只需要传入与上一级差值的指标,映射入实体类
                        collector.collect(TradeSkuOrderBean.builder()
                                .orderDetailId(id)
                                .skuId(skuId)
                                .skuName(skuName)
                                .ts(ts)
                                .originalAmount(newOriginalAmount.subtract(originalAmount))
                                .activityReduceAmount(newActivityReduceAmount.subtract(activityReduceAmount))
                                .couponReduceAmount(newCouponReduceAmount.subtract(couponReduceAmount))
                                .orderAmount(newOrderAmount.subtract(orderAmount))
                                .build());

                        //更新状态
                        lastOrderState.put("originalAmount", newOriginalAmount);
                        lastOrderState.put("activityReduceAmount", newActivityReduceAmount);
                        lastOrderState.put("couponReduceAmount", newCouponReduceAmount);
                        lastOrderState.put("orderAmount", newOrderAmount);
                    }
                });
    }

    private static SingleOutputStreamOperator<JSONObject> getWatermarksStream(SingleOutputStreamOperator<JSONObject> eltStream) {
        return eltStream.assignTimestampsAndWatermarks(WatermarkStrategy
                .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner((event, timestamp) -> event.getLong("ts")));
    }

    private static SingleOutputStreamOperator<JSONObject> elt(DataStreamSource<String> dataStreamSource) {
        return dataStreamSource.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String s, Collector<JSONObject> collector) throws Exception {
                try {
                    //考虑回撤流
                    if (s != null) {
                        JSONObject jsonObject = JSON.parseObject(s);
                        String skuId = jsonObject.getString("sku_id");
                        Long ts = jsonObject.getLong("ts");
                        if (skuId != null && ts != null) {
                            jsonObject.put("ts", ts * 1000);
                            collector.collect(jsonObject);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
